﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessCoordinate
    {

        #region Create
        public static void createCoordinate(Coordinate c)
        {
            try
            {
                dataCoordiante.createCoordinate(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinate");
            }
        }

        public static void createCoordinate(float latitude, float longitude)
        {
            try
            {
                Coordinate c = new Coordinate();
                c.Latitude = latitude;
                c.Longitude = longitude;

                dataCoordiante.createCoordinate(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinate");
            }
        }

        public static void createCoordinateForZone(float latitude, float longitude, Zone zone)
        {
            try
            {
                Coordinate c = new Coordinate();
                c.Latitude = latitude;
                c.Longitude = longitude;
                c.ZoneID = zone.ZoneID;

                dataCoordiante.createCoordinate(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinate for zone");
            }
        }

        public static void createCoordinateForRegion(float latitude, float longitude, Region region)
        {
            try
            {
                Coordinate c = new Coordinate();
                c.Latitude = latitude;
                c.Longitude = longitude;
                c.RegionID = region.RegionID;

                dataCoordiante.createCoordinate(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinate for region");
            }
        }

        public static void createCoordinateForCountry(float latitude, float longitude, Country country)
        {
            try
            {
                Coordinate c = new Coordinate();
                c.Latitude = latitude;
                c.Longitude = longitude;
                c.CountryID = country.CountryID;

                dataCoordiante.createCoordinate(c);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinate for country");
            }
        }
        #endregion

        #region Get
        public static Coordinate getCoodinate(Guid guid)
        {
            try
            {
                return dataCoordiante.getCoodinate(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting coordinate");
            }
        }

        public static List<Coordinate> getCoordinatesByCountry(Country c)
        {
            try
            {
                Expression<Func<Coordinate, bool>> whereClause = (Coordinate co) => (co.CountryID == c.CountryID);

                return dataCoordiante.getCoordinates(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting coordinate by country");
            }
        }

        public static List<Coordinate> getCoordinatesByRegion(Region r)
        {
            try
            {
                Expression<Func<Coordinate, bool>> whereClause = (Coordinate co) => (co.RegionID == r.RegionID);

                return dataCoordiante.getCoordinates(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting coordinate by region");
            }
        }

        public static List<Coordinate> getCoordinatesByZone(Zone z)
        {
            try
            {
                Expression<Func<Coordinate, bool>> whereClause = (Coordinate co) => (co.ZoneID == z.ZoneID);

                return dataCoordiante.getCoordinates(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting coordinate by zone");
            }
        }
        #endregion

        #region Set
        public static Boolean changeCoordinate(Coordinate c)
        {
            try
            {
                if (c == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Coordinate res = db.Coordinates.FirstOrDefault(co => co.CoordinateID == c.CoordinateID);

                    if (res == null) return false;
                    else
                    {
                        res.setCoordinate(c);
                        db.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing coordinate");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteCoordinate(Coordinate c)
        {
            try
            {
                if (c == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    if (db.Coordinates.Contains(c))
                    {
                        Coordinate res = (from co in db.Coordinates
                                          where co.CoordinateID == c.CoordinateID
                                          select co).FirstOrDefault();

                        if (res == null) return false;
                        else
                        {
                            db.Coordinates.DeleteOnSubmit(res);
                            db.SubmitChanges();
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deleting coordinate");
            }
        }
        #endregion
    }
}
